package com.blood.core.resp;

import io.swagger.annotations.ApiModelProperty;

import java.util.Collections;
import java.util.List;


/**
 * @author yelong
 * @version 1.0
 * @date 2020/8/12 11:35
 * @description 分页查询的结果
 */
public final class PageResult<T> {

    /**
     * 查询的页数
     */
    @ApiModelProperty("查询的页数")
    private final int pageNo;
    /**
     * 每页数据条数
     */
    @ApiModelProperty("每页数据条数")
    private final int pageSize;
    /**
     * 总页数
     */
    @ApiModelProperty("总页数")
    private long pageCount;
    /**
     * 总数据条数
     */
    @ApiModelProperty("总数据条数")
    private long totalCount;

    @ApiModelProperty("数据")
    private List<T> data;

    public PageResult(int pageIndex, int pageSize) {
        this.pageNo = pageIndex;
        this.pageSize = pageSize;
    }

    public PageResult() {
        pageNo = 1;
        pageSize = 10;
    }

    public static <T> PageResult<T> create(int pageIndex, int pageSize) {
        return new PageResult<>(pageIndex, pageSize);
    }

    public static <T> PageResult<T> create(PageParameter parameter) {
        return new PageResult<>(parameter.getPageIndex(), parameter.getPageSize());
    }

    /**
     * 设置数据集及总数
     *
     * @param totalCount
     * @param data
     * @return
     */
    public PageResult<T> data(long totalCount, List<T> data) {
        this.totalCount = totalCount;
        this.data = Collections.unmodifiableList(data);
        this.pageCount = totalCount % pageSize == 0 ? (totalCount / pageSize) : (totalCount / pageSize + 1);
        return this;
    }

    public List<T> getData() {
        return data;
    }

    public int getPageNo() {
        return pageNo;
    }

    public int getPageSize() {
        return pageSize;
    }

    public long getPageCount() {
        return pageCount;
    }

    public long getTotalCount() {
        return totalCount;
    }

    public static void main(String[] args) {
        long totalCount = 0;
        int pageSize = 10;
        int pageNo = 1;
        long count = 0;
        count = totalCount / pageSize;
        System.out.println(count);


        count = totalCount % pageSize == 0 ? (totalCount / pageSize) : (totalCount / pageSize + 1);
        System.out.println(count);
    }
}
